package question01;

import java.util.HashMap;

/**
 * @author yuan
 * @date 2021/11/25
 */
public class Solution {
    public static void main(String[] args) {
        int[] test = {2, 7, 11, 15};
        int target = 9;
        int[] ints = twoSum(test, target);
        for (int anInt : ints) {
            System.out.println(anInt);
        }
    }
    public static int[] twoSum(int[] nums, int target) {
        int[] res = new int[2];
        if (nums.length == 0 || nums == null) return res;
        HashMap<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            int temp = target - nums[i];
            if (map.containsKey(temp)) {
                res[0] = map.get(temp);
                res[1] = i;
            }
            map.put(nums[i], i);
        }

        return res;
    }
}
